<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<!--导航栏-->
		<pageLayout
			:barTitle="title"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<view @touchmove.stop.prevent>
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="岗位" v-if="!userStore?.kpiPostsId">
							<uni-data-select
								v-model="searchModel.crmPostId"
								placeholder="请选择岗位"
								:localdata="kpiPostList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="指标年月">
							<picker mode="date" fields="month" :value="searchModel.targetMonth" @change="targetMonthChange">
								<ty-picker
									placeholder="请选择指标年月"
									@clear="clearPicker"
									:value="searchModel.targetMonth"></ty-picker>
							</picker>
						</uni-forms-item>
						<uni-forms-item label="指标名称">
							<uni-easyinput v-model="searchModel.targetName" placeholder="请输入指标名称" />
						</uni-forms-item>
						<uni-forms-item label="用户账号" v-if="!userStore?.kpiPostsId">
							<uni-easyinput v-model="searchModel.userName" placeholder="请输入用户账号" />
						</uni-forms-item>
						<uni-forms-item label="用户姓名" v-if="!userStore?.kpiPostsId">
							<uni-easyinput v-model="searchModel.nickName" placeholder="请输入用户姓名" />
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
			</view>
		</up-popup>
		<view class="content">
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					@row-click="rowClick"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					stripe
					border>
					<template v-slot:left="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">
								岗位：
								<text class="table-td-text">{{arrText(kpiPostList, row.crmPostId)}}</text></view>
							<view class="table-td-row">
								账号：
								<text class="table-td-text">{{row.userName}}</text></view>
							<view class="table-td-row">
								姓名：
								<text class="table-td-text">{{row.nickName}}</text></view>
						</view>
					</template>
					
					<template v-slot:center="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">
								名称：
								<text class="table-td-text">{{row.targetName}}</text></view>
							<view class="table-td-row">
								年月：
								<text class="table-td-text">{{row.targetMonth}}</text></view>
							<view class="table-td-row">
								占比：
								<text class="table-td-text">{{row.proportion * 100}}%</text></view>
						</view>
					</template>
					<template v-slot:right="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">
								指标：
								<text class="table-td-text">{{row.targetValue}}</text></view>
							<view class="table-td-row">
								完成：
								<text class="table-td-text">{{row.completionValue}}({{Math.floor((row.completionValue / row.targetValue) * 100)}}%)</text></view>
							<view class="table-td-row">
								考核说明：
								<text class="table-td-text">{{row.remark}}</text></view>
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref, getCurrentInstance } from 'vue'
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { kpiPost, kpiPostSchedule } from "@/api/erp_api/kpi"
import { isObjectEmptySimple, copyText, arrText } from '@/utils/ty.js'
import useUserStore from '@/store/modules/user'
import { timeFormat } from 'uview-plus'

	const userStore = useUserStore()
	const { proxy } = getCurrentInstance()
	
	// 标题设置
	const title = ref('')
	const kpiPostsId = ref('')
	const userId = ref('')
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		userId.value = option.userId ? option.userId : ''
		if (option.kpiPostsId) {
			kpiPostsId.value = option.kpiPostsId
			searchModel.value.crmPostId = option.kpiPostsId
		}
		uni.setNavigationBarTitle({
			title: title.value 
		})
	})
	
	// 岗位
	const kpiPostList = ref([])
	const getKpiPost = () => {
		return kpiPost().then(res => {
			kpiPostList.value = res.rows.map(item => {
				return {
					...item,
					value: item.id,
					text: `${item.userPostName}【${item.level}】`
				}
			}) || []
		})
	}
	
	// 选择年月
	const targetMonthChange = (e) => {
		searchModel.value.targetMonth = e.detail.value
	}
	const clearPicker = () => {
		searchModel.value.targetMonth = ''
	}
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		crmPostId: '',
		targetMonth: timeFormat(new Date(), 'y-m'),
		nickName: '',
		userName: '',
		targetName: ''
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			crmPostId: '',
			targetMonth: timeFormat(new Date(), 'y-m'),
			nickName: '',
			userName: '',
			targetName: ''
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '用户信息', key: 'left' },
		{ title: '指标信息', key: 'center' },
		{ title: '完成情况', key: 'right' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		if (searchModel.value.targetName) {
			params.targetName = searchModel.value.targetName
		}
		if (searchModel.value.targetMonth) {
			params.targetMonth = searchModel.value.targetMonth.replace('-', '')
		}
		if (userId.value) {
			params.completionUser = userId.value
		} else if (kpiPostsId.value) {
			params.crmPostId = kpiPostsId.value
			params.completionUser = userStore.user.userId
		} else if (userStore.kpiPostsId) {
			params.crmPostId = userStore.kpiPostsId
			params.completionUser = userStore.user.userId
		}
		if (searchModel.value.crmPostId) {
			params.crmPostId = searchModel.value.crmPostId
		}
		if (searchModel.value.userName) {
			params.userName = searchModel.value.userName
		}
		if (searchModel.value.nickName) {
			params.nickName = searchModel.value.nickName
		}
		// 判断段当前用户
		if (!checkRole(['admin'])) {
			// params.completionUser = userStore.user.userId
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		kpiPostSchedule(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 点击行
	const rowClick = (row) => {
		
	}
 	
	onMounted(async () => {
		await getKpiPost()
		searchFirst()
	})
</script>

<style scoped lang="scss">
</style>
